package cn.smart.epic.common.controller;

import cn.smart.epic.common.constant.state.Order;
import cn.smart.epic.common.constant.tips.SuccessTip;
import cn.smart.epic.common.page.PageReq;
import cn.smart.epic.common.warpper.BaseControllerWarpper;
import cn.smart.epic.core.support.HttpKit;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Auther Uncle_Z
 * @date 2017/9/19 23:21
 * @Email:2545375@qq.com
 * @Descrition
 * @since 1.0
 */

public class BaseController {

    protected static String SUCCESS = "SUCCESS";
    protected static String ERROR = "ERROR";

    protected static String REDIRECT = "redirect:";
    protected static String FORWARD = "forward:";

    protected static SuccessTip successTip = new SuccessTip();

    protected HttpServletRequest getHttpServletRequest(){
        return HttpKit.getRequest();
    }

    protected HttpServletResponse getHttpServletResponse(){
        return HttpKit.getResponse();
    }

    protected HttpSession getSession() {
        return HttpKit.getRequest().getSession();
    }

    protected HttpSession getSession(boolean flag){
        return HttpKit.getRequest().getSession(flag);
    }

    protected String getPara(String name) {
        return HttpKit.getRequest().getParameter(name);
    }

    protected void setAttr(String name, Object value) {
        HttpKit.getRequest().setAttribute(name, value);
    }

    public PageReq defaultPage(){
        HttpServletRequest request = HttpKit.getRequest();
        int limit = Integer.valueOf(request.getParameter("limit"));
        int offset = Integer.valueOf(request.getParameter("offset"));
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        PageReq pageReq = new PageReq(limit, offset, sort, order);
        if (StringUtils.isEmpty(order)) {
            pageReq.setOpenSort(false);
        }else{
            if (Order.ASC.getDes().equals(order)) {
                pageReq.setAsc(true);
            }else{
                pageReq.setAsc(false);
            }
        }
        return pageReq;
    }

    /**
     * 包装一个list，让list增加额外属性
     * @param warpper
     * @return
     */
    protected Object warpObject(BaseControllerWarpper warpper){
        return warpper.warp();
    }
}
